<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons");
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "400px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    dataInfo: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
      data: [],
      dataName: []
    };
  },
  watch: {
    dataInfo: {
      deep: true,
      handler(val) {
        this.coverData();
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      // this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    coverData() {
      this.data = [
        this.dataInfo.contractMoney,
        this.dataInfo.sum90,
        this.dataInfo.sum60,
        this.dataInfo.sum30,
        this.dataInfo.profit,
        this.dataInfo.commission,
      ];
      this.dataName = [
        "成交金额",
        "90%项目金额",
        "60%项目金额",
        "30%项目金额",
        "预计利润",
        "预计提成",
      ]

      if(this.dataInfo.cashFlow || this.dataInfo.cashFlow === 0){
        this.data.push(this.dataInfo.cashFlow)
        this.dataName.push("现金流")
      }
      this.$nextTick(() => {
        this.initChart();
      });
    },
    formatNumber(value) {
      // Simplify number display
      if (value >= 1e6) return (value / 1e6).toFixed(1) + "M"; // Millions
      if (value >= 1e3) return (value / 1e3).toFixed(1) + "K"; // Thousands
      return value.toString();
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions();
      this.chart.on("click", (params) => {
        let progess = "";
        if (params.name === "成交金额") {
          progess = "5";
        } else if (params.name === "90%项目金额") {
          progess = "4";
        } else if (params.name === "60%项目金额") {
          progess = "3";
        } else if (params.name === "30%项目金额") {
          progess = "2";
        } else {
          return;
        }
        this.$emit("toIndex", progess);
      });
    },
    setOptions({ expectedData, actualData } = {}) {
      const data = this.data;
      this.chart.setOption({
        title: {
          text: "业绩数据",
          left: "left",
          textStyle: {
            fontSize: 14,
          },
        },
        tooltip: {
          trigger: "item",
          formatter: (params) => {
            return `类别: ${params.name}<br>值: ${params.value}`;
          },
        },
        grid: {
          top: "10%",
          left: "1%",
          right: "1%",
          bottom: "10%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.dataName,
          axisLabel: {
            fontSize: 10,
            interval: 0,
            rotate: 30,
          },
        },
        yAxis: {
          type: "value",
          axisLabel: {
            fontSize: 10,
          },
        },
        series: [
          {
            data: data,
            type: "line",
            areaStyle: {},
            itemStyle: {
              color: "#409eff",
            },
          },
        ],
      });
    },
  },
};
</script>
